<?php

use Illuminate\Support\Facades\Cache;

function renderSuccess($data = [], $msg = 'success', $code = 200)
{
    return response()->json([ 'data' => $data, 'msg' => $msg, 'code' => $code]);
}

function renderError($msg = 'error', $code = 400)
{
    return response()->json(['msg' => $msg, 'code' => $code]);
}


// 校验验证码
function checkCaptcha($mobile, $code)
{
    $key = 'captcha-' . $mobile;
    $value = Cache::get($key);
    if (!$value) {
        return false;
    }
    if ($value['code'] != $code) {
        return false;
    }
    return true;
}

function integralLog($user_id, $integral, $type, $desc)
{
    $data = [
        'user_id' => $user_id,
        'integral' => $integral,
        'type' => $type,
        'remark' => $desc,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s')
    ];
    \App\Models\IntegralLog::insert($data);
    // 更新用户积分
    if ($type == 1) {
        \App\Models\User::where('id', $user_id)->increment('integral', $integral);
    } else {
        \App\Models\User::where('id', $user_id)->decrement('integral', $integral);
    }
}
